/*---------------------------------------------------------------------------
//
//	Copyright(C) SMIT Corporation, 2000-2010.
//
//  File	:	sm_types.h
//	Purpose	:	CIBOX Base Type Declare
//	History :
//				2004-09-09 created by pmy
//
---------------------------------------------------------------------------*/


#ifndef __SM_TYPES_H
#define __SM_TYPES_H

#ifdef __cplusplus
extern "C"{
#endif

/****************************************************************************
*
*	types define
*
****************************************************************************/

#ifndef NULL
#define NULL 0
#endif

//typedef int					BOOL;
#ifndef	BOOL
#define BOOL	int
#endif

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef OK
#define OK	0
#endif

/****************************************************************************
*
*	Macros define
*
****************************************************************************/

#ifndef LOBYTE
#define LOBYTE(w)           ((unsigned char)((w) & 0xFF))
#endif

#ifndef HIBYTE
#define HIBYTE(w)           ((unsigned char)(((unsigned short)(w) >> 8) & 0xFF))
#endif

#define SM_MIN(a,b)			(((a)<=(b))?(a):(b))
#define SM_MAX(a,b)			(((a)>=(b))?(a):(b))

#define SAFE_PSET(ptr, value)	{if(ptr) *ptr = value;}

#ifndef countof
#define countof(array) (sizeof(array)/sizeof(array[0]))
#endif

#define SM_MACRO_START do {
#define SM_MACRO_END   } while (0)

#ifndef EMPTY_STATEMENT
#define EMPTY_STATEMENT SM_MACRO_START SM_MACRO_END
#endif

#ifndef BIT
#define BIT(n) (0x01uL << (n))
#ifndef BIT0
#define BIT0  0x00000001uL
#define BIT1  0x00000002uL
#define BIT2  0x00000004uL
#define BIT3  0x00000008uL
#define BIT4  0x00000010uL
#define BIT5  0x00000020uL
#define BIT6  0x00000040uL
#define BIT7  0x00000080uL
#endif //BIT0
#define BIT8  0x00000100uL
#define BIT9  0x00000200uL
#define BIT10 0x00000400uL
#define BIT11 0x00000800uL
#define BIT12 0x00001000uL
#define BIT13 0x00002000uL
#define BIT14 0x00004000uL
#define BIT15 0x00008000uL
#define BIT16 0x00010000uL
#define BIT17 0x00020000uL
#define BIT18 0x00040000uL
#define BIT19 0x00080000uL
#define BIT20 0x00100000uL
#define BIT21 0x00200000uL
#define BIT22 0x00400000uL
#define BIT23 0x00800000uL
#define BIT24 0x01000000uL
#define BIT25 0x02000000uL
#define BIT26 0x04000000uL
#define BIT27 0x08000000uL
#define BIT28 0x10000000uL
#define BIT29 0x20000000uL
#define BIT30 0x40000000uL
#define BIT31 0x80000000uL
#endif //BIT

#ifndef NIB0
#define NIB0 ((unsigned char)(BIT0  | BIT1  | BIT2  | BIT3))
#define NIB1 ((unsigned char)(BIT4  | BIT5  | BIT6  | BIT7))
#define NIB2 ((unsigned short)(BIT8  | BIT9  | BIT10 | BIT11))
#define NIB3 ((unsigned short)(BIT12 | BIT13 | BIT14 | BIT15))
#define NIB4 ((unsigned long)(BIT16 | BIT17 | BIT18 | BIT19))
#define NIB5 ((unsigned long)(BIT20 | BIT21 | BIT22 | BIT23))
#define NIB6 ((unsigned long)(BIT24 | BIT25 | BIT26 | BIT27))
#define NIB7 ((unsigned long)(BIT28 | BIT29 | BIT30 | BIT31))
#endif //NIB0

#ifndef BYTE0
#define BYTE0 (NIB0 | NIB1)
#define BYTE1 (NIB2 | NIB3)
#define BYTE2 (NIB4 | NIB5)
#define BYTE3 (NIB6 | NIB7)
#endif //BYTE0

#ifndef WORD0
#define WORD0 (BYTE0 | BYTE1)
#define WORD1 (BYTE2 | BYTE3)
#endif //WORD0

// HAL read/write
#define SM_HAL_READ_BYTE( _register_ )   *((volatile unsigned char *)(_register_))
#define SM_HAL_WRITE_BYTE( _register_, _value_ )   (*((volatile unsigned char *)(_register_)) = (_value_))
#define SM_HAL_READ_UINT32( _register_ )    *((volatile unsigned long *)(_register_))
#define SM_HAL_WRITE_UINT32( _register_, _value_ )  (*((volatile unsigned long *)(_register_)) = (_value_))

#ifdef __cplusplus
}
#endif

#endif // __SM_TYPES_H
